<?php

	require_once 'lib/common.php';
	
	$configExists = file_exists('bmconfig.php');
	
	// In case the configuration file exists
	// open it so the existing settings can be loaded
	if ($configExists)
		require_once 'bmconfig.php';
		
	$passStoredAsHash = defined('ADMIN_PASSWORD_ISMD5') && ADMIN_PASSWORD_ISMD5; 

	function makeConfigFile()
	{
		$homedir =  isset($_POST['homedir']) ? $_POST['homedir'] : exit('Home Directory cannot be empty');
		$user    = (isset($_POST['user']) && (!empty($_POST['user']))) ? $_POST['user'] : exit('Username cannot be blank');
		$pass1   = (isset($_POST['pass1']) && (!empty($_POST['pass1']))) ? $_POST['pass1'] : exit('Password cannot be blank');
		$pass2   = (isset($_POST['pass2']) && (!empty($_POST['pass2']))) ? $_POST['pass2'] : exit('Both password fields must be filled');
        $autoLogout = isset($_POST['autologout']) && is_numeric($_POST['autologout']) ? $_POST['autologout'] : exit('Auto logout interval is invalid');   
		$theme	 =  isset($_POST['theme']) ? strtolower($_POST['theme']) : 'light';
		
		if ($pass1 != $pass2)
			exit('Passwords don\'t match');
		
		$hashPass = isset($_POST['hashpass']) ? 'true' : 'false';

		if (isset($_POST['hashpass']))
			$pass1 = md5($pass1);
		
		$zebraGrids = boolToStr(isset($_POST['zebragrids']));
		
	
		$config = <<< HTML
<?php
    define('BM_AUTH', 'bm_auth');
    define('BM_LAST_ACTIVITY', 'bm_last_activity');
    define('BM_COPY', 'bm_copy');
    define('BM_CLIPBOARD', 'bm_clipboard');
    
    define('HOMEDIR', '$homedir');
    define('ADMIN_USERNAME', '$user');
    define('ADMIN_PASSWORD', '$pass1');
    define('ADMIN_PASSWORD_ISMD5', $hashPass);
    global \$restrictedFolders;
    \$restrictedFolders = array ();
    define('ZEBRA_GRIDS', $zebraGrids);
	define('ACTIVE_THEME', '$theme');
	define('AUTO_LOGOUT', $autoLogout);
HTML;

		file_put_contents('bmconfig.php', $config) or exit('Unable to save configuration file');
		
		exit('Settings successfully saved');
	}
	
	if (isset($_GET['do'])):
		exit(makeConfigFile());
	else:
		initSite('Configuration', 'Change settings'); 
		
		$theme = defined('ACTIVE_THEME') ? ACTIVE_THEME : 'light';
	?>
		
		</div>
		<script type='text/javascript'>
			function submitForm()
			{
				showCustomMsg('Saving configuration');
			
				$.ajax({
					url: 'config.php?do',
					type: 'POST',
					data: $("#frmconfig").serialize(),
					success: function(e){
						showCustomMsg(e)
					},
					error: function(e){
						showCustomMsg(e)
					}
				});
			}
		</script>
		<br><br>
		<div class='infodiv center' style="width: 700px;">
			<div class='title'><?php echo $configExists ? 'Edit' : 'Create' ?> configuration settings</div>
			<form class="flatform" id="frmconfig" action='?do' method='post'>
				<table>
					<tr>
						<td>Home path</td>
						<td>
							<input type='text' name='homedir' value='<?php echo defined('HOMEDIR') ? htmlentities(HOMEDIR) : './' ?>'> 
							<span class='gray'>Relative to BitManager's home directory</span>
						</td>
					</tr>
					<tr>
						<td>Administrator username</td>
						<td><input type='text' name='user' value='<?php echo defined('ADMIN_USERNAME') ? htmlentities(ADMIN_USERNAME) : '' ?>'></td>
					</tr>
					<tr>
						<td>Password</td>
						<td><input type='password' name='pass1' value='<?php echo defined('ADMIN_PASSWORD') ? ($passStoredAsHash ? '' : htmlentities(ADMIN_PASSWORD)) : '' ?>'></td>
					</tr>
					<tr>
						<td>Repeat password</td>
						<td><input type='password' name='pass2' value='<?php echo defined('ADMIN_PASSWORD') ? ($passStoredAsHash ? '' : htmlentities(ADMIN_PASSWORD)) : '' ?>'></td>
					</tr>
					<tr>
						<td>Store password as hash</td>
						<td><input type='checkbox' name='hashpass' <?php echo defined('ADMIN_PASSWORD_ISMD5') ? (ADMIN_PASSWORD_ISMD5 ? 'checked="true"' : '') : '' ?>></td>
					</tr>
					<tr>
						<td>Theme</td>
						<td>
							<select name='theme'>
								<option <?php if ($theme == 'dark') echo 'selected'; ?> >Dark</option>
								<option <?php if ($theme == 'light') echo 'selected'; ?> >Light</option>
							</select>
						</td>
					</tr>
					<tr>
						<td>Auto logout</td>
						<td>
							<input type='text' name='autologout' <?php echo defined('AUTO_LOGOUT') ? 'value="'. AUTO_LOGOUT . '"' : '' ?>>
							<span class='gray'>Log out automatically if inactive for more than specified minutes</span>
						</td>
					</tr>
					<tr>
						<td>Zebra grids for file list</td>
						<td><input type='checkbox' name='zebragrids' <?php echo defined('ZEBRA_GRIDS') ? (ZEBRA_GRIDS ? 'checked="true"' : '') : '' ?>></td>
					</tr>
					<tr>
						<td></td>
						<td><input class="hrefbtn" type='button' value='Save settings' onclick='submitForm();'>
							&nbsp;<input class="hrefbtn" type='button' value='Return to bitmanager' onclick='window.location="."'></td>
					</tr>
				</table>
			</form>
		</div>
		
		
		<?php finishSite();
	endif;